package com.tanhua.dubbo.server.api;


import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.enums.DescriptionEnum;
import com.tanhua.dubbo.server.enums.IdType;
import com.tanhua.dubbo.server.enums.OperatingEnum;
import com.tanhua.dubbo.server.pojo.Operation;
import com.tanhua.dubbo.server.service.IdService;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

@Service(version = "1.0.0")
@Slf4j
public class OperationApiImpl implements OperationApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdService idService;


    @Override
    public Boolean queryOperation(OperatingEnum operatingEnum, Long id, DescriptionEnum descriptionEnum) {
        try {
            
            //String username = BackToken.getUsername();
//            String ip = request.getRemoteAddr();
            Operation operation = new Operation();
            operation.setId(ObjectId.get());
            operation.setIp("192.168.0.81");
//        operation.setUsername(username);
            operation.setUsername("张三");
            operation.setContent(operatingEnum.getValue() == 1 ? "删除内容" : (operatingEnum.getValue() == 2 ? "审核通过" : "审核不通过"));
            operation.setDescription(descriptionEnum.getValue() == 1 ? "头像审核" : (operatingEnum.getValue() == 2 ? "动态审核" : "视频审核") );
            operation.setOperatingId(id);
            operation.setLogId(idService.createId(IdType.OPERATION));
            operation.setCreated(System.currentTimeMillis());
            Operation save = mongoTemplate.save(operation);

            return true;
        } catch (Exception e) {
            log.error("用户运营日志表写入失败" + e);
        }

        return false;
    }

    @Override
    public List<Operation> getOperations(Integer page, Integer pageSize, Class entityClass) {


        Pageable pageable = PageRequest.of(page - 1, pageSize,
                Sort.by(Sort.Order.desc("created")));

        Query query = new Query().with(pageable);
        List<Operation> operationList = mongoTemplate.find(query,Operation.class);
        return operationList;
    }

    @Override
    public List<Operation> queryPageOperations(String username, String content, Long startTime,Long endTime, Integer page, Integer pageSize) {
        Pageable pageable = PageRequest.of(page - 1, pageSize,
                Sort.by(Sort.Order.desc("created")));

        Criteria criteria = new Criteria();
        if (ObjectUtil.isNotEmpty(username)&&username.length()<=12&&username.length()>=0){
            criteria.and("username").regex(".*" +username+".*");
        }
        if (ObjectUtil.isNotEmpty(content)&&content.length()<=12&&content.length()>=0){
            criteria.and("content").regex(".*" +content+".*");
        }
        if (ObjectUtil.isNotEmpty(startTime)&& ObjectUtil.isNotEmpty(endTime)){
            DateTime dateStart = new DateTime(startTime);
            //一天的开始，结果：2017-03-01 00:00:00
            Date beginOfDay = DateUtil.beginOfDay(dateStart);
            long begin = beginOfDay.getTime();
            DateTime dateEnd = new DateTime(endTime);
            //一天的结束，结果：2017-03-01 23:59:59
            Date endOfDay = DateUtil.endOfDay(dateEnd);
            long end = endOfDay.getTime();
            criteria.and("created").lte(end).gte(begin);
        }
        Query query = Query.query(criteria).with(pageable);
        List<Operation> operationList = mongoTemplate.find(query,Operation.class);

        return operationList;
    }

}
